/****************************************************
	文件：AssetAnalyze.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/8/31 15:55:59
	功能：文件依赖、引用关系查找
*****************************************************/

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace HTFramework.Utility.Editor
{
    public class AssetAnalyze
    {
        /// <summary>
        /// 查找引用
        /// </summary>
        [MenuItem("Assets/查找引用", false, 1)]
        static void MenuClick()
        {
            Debug.Log("开始查找有引用该文件的文件..");
            EditorSettings.serializationMode = SerializationMode.ForceText;
            string path = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (!string.IsNullOrEmpty(path))
            {
                string guid = AssetDatabase.AssetPathToGUID(path);
                var withoutExtensions = new List<string>() {".prefab", ".unity", ".mat", ".asset"};
                string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
                    .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
                int startIndex = 0;
                EditorApplication.update = delegate()
                {
                    string file = files[startIndex];

                    bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file,
                        1.0f * startIndex / files.Length);
                    if (Regex.IsMatch(File.ReadAllText(file), guid))
                    {
                        Debug.Log(file, AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
                    }

                    startIndex++;
                    if (isCancel || startIndex >= files.Length)
                    {
                        EditorUtility.ClearProgressBar();
                        EditorApplication.update = null;
                        startIndex = 0;
                        Debug.Log("查找结束!");
                    }
                };
            }
        }

        /// <summary>
        /// 查找依赖
        /// </summary>
        [MenuItem("Assets/查找依赖", false, 2)]
        static private void FindDependencies()
        {
            Debug.Log("开始查找该文件依赖的文件..");
            string path = AssetDatabase.GetAssetPath(Selection.activeObject);
            string[] files = AssetDatabase.GetDependencies(path);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Equals(path))
                {
                    continue; //排除自身
                }

                Debug.Log(files[i], AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(files[i])));
            }

            Debug.Log("查找结束!");
        }

        static string GetRelativeAssetsPath(string path)
        {
            return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "")
                .Replace('\\', '/');
        }
    }
}